package com.gem.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.back.entity.AoaDept;
import com.gem.back.entity.AoaNoticeUserRelation;
import com.gem.back.entity.AoaPosition;
import com.gem.back.entity.AoaTypeList;
import com.gem.back.mapper.AoaDeptMapper;
import com.gem.back.service.IAoaDeptService;
import com.gem.back.service.IAoaPositionService;
import com.gem.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hhhy
 * @since 2020-02-22
 */
@Controller
@RequestMapping("/position")
public class AoaPositionController {
    @Autowired
    private IAoaPositionService AoaPositionService;
    @Autowired
    private IAoaDeptService aoaDeptService;
    @Autowired
    private IAoaPositionService aoaPositionService;

    @RequestMapping("/positionmanage")
    public String positionManage(Model model){
        List<AoaPosition> aoaPositionList = AoaPositionService.list();
        model.addAttribute("positionList",aoaPositionList);
        return "user/positionmanage";
    }

    @RequestMapping("/positionedit")
    public String positionedit(Integer positionId,AoaPosition aoaPosition,AoaDept aoaDept, Model model){
        if (positionId != null) {
            aoaPosition = AoaPositionService.getById(positionId);
        }
        QueryWrapper<AoaDept> queryWrapper =new QueryWrapper<>();
        if (aoaDept != null && aoaDept.getDeptName() != null && aoaDept.getDeptName() != "") {
            queryWrapper.eq("dept_name", aoaDept.getDeptName());
        }
        List<AoaDept> deptList =  aoaDeptService.list(queryWrapper);
        model.addAttribute("deptList",deptList);
        model.addAttribute("position",aoaPosition);
        return "user/positionedit";
    }

    @RequestMapping("/save")
    public String saveOrUpdate(AoaPosition aoaPosition){
        AoaPositionService.saveOrUpdate(aoaPosition);
        return "redirect:positionmanage";
    }

    //删除职位管理
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult deletePosition(Integer positionId){
        boolean flag = AoaPositionService.removeById(positionId);
        ResponseResult result = null;
        if (flag == true){
            result = new ResponseResult("200","删除成功！");
        }else{
            result = new ResponseResult("200","删除失败！");
        }
        return result;
    }

    @RequestMapping("/delposition")
    public String delposition(Integer positionId){
        aoaPositionService.removeById(positionId);
        return "redirect:positionmanage";
    }
    //逻辑删除职位列表
//    @ResponseBody
//    @RequestMapping("/deletePosition")
//    public ResponseResult deletePositionList(Integer positionId){
//        AoaPosition aoaPosition = aoaPositionService.findOnePositionById(positionId);
//        aoaPosition.setDelFlag(1);
//        aoaPositionService.updateById(aoaPosition);
//        System.out.println("================================>"+aoaPosition.getDelFlag());
//        ResponseResult result = null;
//        if(aoaPosition.getDelFlag() == 1){
//            result = new ResponseResult("200","删除成功！");
//        }else{
//            result = new ResponseResult("200","删除失败！");
//        }
//        return result;
//    }

}
